﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JinglunIdrRead
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtSex.Text = "";
            txtMinzu.Text = "";
            txtBirth.Text = "";
            txtAddress.Text = "";
            txtIdCard.Text = "";
            txtFrom.Text = "";
            txtValidTo.Text = "";
            p_img.BackgroundImage = null;
            byte[] pMsg = JinglunApi.readIDCard();
            if (pMsg.Length<1)
            {
                return;
            }
            
            string name = getBytes(pMsg, 0, 31);
            string sex = getBytes(pMsg, 31, 3);
            string minzhu = getBytes(pMsg, 34, 10);
            string birth = getBytes(pMsg, 44, 9);
            string address = getBytes(pMsg, 53, 71);
            string IDCard = getBytes(pMsg, 124, 19);
            string from = getBytes(pMsg, 143, 31);
            string startTime = getBytes(pMsg, 174, 9);
            string endTime = getBytes(pMsg, 183, 9);
            txtName.Text = name;
            txtSex.Text = sex;
            txtMinzu.Text = minzhu;
            txtBirth.Text = birth;
            txtAddress.Text = address;
            txtIdCard.Text = IDCard;
            txtFrom.Text = from;
            StringBuilder sb = new StringBuilder(startTime);
            txtValidTo.Text =sb.Append("=="+endTime)+"";
            p_img.BackgroundImage = Image.FromFile("photo.bmp");
        }

        private int port = 6000;
        private string host = "127.0.0.1";
        private Socket sSocket;
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);

                sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sSocket.Bind(ipe);
                sSocket.Listen(0);
                Console.WriteLine("监听已经打开，请等待");

                Thread listenThread = new Thread(new ThreadStart(listenSocket));
                listenThread.Start();
            }
            catch (Exception)
            {

                MessageBox.Show("程序启动失败,请查看端口6000是否被占用!!!");
                Application.Exit();
            }
           
        }

        private void listenSocket()
        {
            Console.WriteLine("socket监听线程启动");
            try
            {
                while (true)
                {
                    Socket serverSocket = sSocket.Accept();
                    serverSocket.ReceiveTimeout=2000;
                    try
                    {

                        Console.WriteLine("连接已经建立");

                        byte[] recByte = new byte[4096];
                        byte[] msg = new byte[1];
                        int read = serverSocket.Receive(msg, 1, 0);
                        byte b = msg[0];
                        Console.WriteLine("服务器获取到数据：" + b);
                        if (b == 1)
                        {
                            byte[] pMsg = JinglunApi.readIDCard();
                            serverSocket.Send(pMsg);
                        }
                        else if (b == 2)
                        {
                            byte[] bs = JinglunApi.readIDCardSn();
                            serverSocket.Send(bs);
                        }
                        else if (b == 3) {
                            byte[] bs = JinglunApi.readICCardSn();
                            serverSocket.Send(bs);
                        }
                        else if (b == 99)
                        {

                            break;
                        }
                        serverSocket.Close();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("等待连接时发生错误" + e);

                    }
                }
            }
            catch (Exception e)
            {
                
               Console.WriteLine("服务器监听时发生错误" + e);
            }
            Application.Exit();
        }

        private string getBytes(byte[] pMsg, int start, int size){
		    byte[] bs=new byte[size];
		    Array.Copy(pMsg, start, bs, 0, size);
            string s = System.Text.Encoding.Default.GetString(bs).Trim();
		    return s;
	    }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] bs=JinglunApi.readIDCardSn();
            txtSn.Text = "";
            if (bs.Length < 1)
            {
                return;
            }
            StringBuilder sb = new StringBuilder();
            foreach (byte item in bs)
            {
                char c = (char)item;
                sb.Append(c);
            }
            txtSn.Text = sb.ToString();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            FileInfo f = new FileInfo("open.txt");
            if (!f.Exists)
            {
                Hide();
            }
            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            sSocket.Close();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }
    }
}
